"""日志类视图函数"""
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from common.db_util import MySqlDb
from common.http_util import Result

'''操作日志表'''
def to_user_login_log(request):
    return render(request,'page/system/sys_user_login_log.html')

'''查询用户操作日志表：带分页'''
class UserLoginLogList(APIView):
    # get请求
    def get(self,request):
        #sql查询语句
        page = None
        page_size = None
        dict1 = {}
        try:
            page = int(request.GET.get("page"))
            page_size = int(request.GET.get("page_size"))
            print("========page:",page,"page_size:",page_size)
            user_name=request.GET.get("user_name")
            if user_name is not None:
                dict1["user_name"] = user_name
                status=request.GET.get("status")
            status =request.GET.get("status")
            if status is not None:
                dict1["status"] = status
        except Exception as e:
            print("参数异常：",e)
        if page is None:
            page = 1
        if page_size is None:
            page_size = 10

        #查询用户日志
        sql = "select * from sys_user_login_log where 1=1 "
        if dict1 is not None and len(dict1) > 0:
            for key,value in dict1.items():
                if value is not None and value != "":
                    sql += " and "+key+" = :"+key
        sql += " order by create_date desc"
        print("sql:",sql)
        log_dict = MySqlDb.query_by_page(sql,page=page,page_size=page_size,params=dict1)
        result = Result(code=0,msg="查询成功",data=log_dict)
        return Response(result)